import java.util.Arrays;
public class test {
    //数组转字符串   //返回值类型为字符串，
    public static String myTOString(int[] array){
        String ret = "[";
        for (int i = 0; i <array.length ; i++) {
            ret += array[i];
            if(i != array.length-1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main(String[] args) {
        int[] r ={1,2,3,4,5};
        String ret = myTOString(r);
        System.out.println(ret);
    }
    public static String myString(int[] array) {  //方法中也得创建变量；
        String ret = "[";
        for (int i = 0; i <array.length ; i++) {
            ret += array[i];
        }
        ret = "]";
        return ret;
    }
    public static void main5(String[] args) {
        int[] r={1,2,3,4,5};
        String ret = myString(r);
        System.out.println(ret);
    }
    //数组作为返回值
    public static int[] func(){
        int[] ret = {1,2,3,4,5};
       // return new int[]{1,2,3,4,5};
        return ret;
    }
    public static void main4(String[] args) {
        int[] r = func();
        for (int i = 0; i <r.length ; i++) {
            System.out.print(r[i]+" ");//注意!!!!!括号中不能只写i；
        }
    }
    //数组作为参数进行传参
    public static void test1(int[] array){
        array = new int[] {10,20,30,40,50};//此时形参指向了一个新的对象，但实参并没有指向一个新的对象。
        System.out.println(Arrays.toString(array));
    }
    public  static void test2(int[] array){
        array[0] = 99;
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        test1(array);
        test2(array);
        System.out.println(Arrays.toString(array));
    }
    //遍历数组的三种方法
    public static void main1(String[] args) {
        //for-each循环
        int[] array = {1,2,3,4,5};
        for (int x:array){
            System.out.print(x+" ");//用x的值来依次接收数组中的每一个值
        }
        System.out.println();
        System.out.println("================");
        for (int i = 0; i <array.length ; i++) {
            System.out.print(array[i]+" ");
        }
        String ret = Arrays.toString(array);//toString这个方法的返回值类型为string,将传入的字符串以数组的形式输出
        System.out.println();
        System.out.println(ret);
    }
}
